using System;
using System.Data;
using System.Data.SqlClient;

using Sunyi.Project.WebPoint.Common;
using Sunyi.Project.WebPoint.DataAccess;

using Sunyi.Project.WebPoint.DataContract;

namespace Sunyi.Project.WebPoint.DataAccess
{
    public partial class SystemInfoDao : IDao<SystemInfo>
    {
        public SystemInfo Select(Guid id)
        {
            SystemInfo systemInfo = null;

            SqlConnection connection = ConnectionProvider.CreateConnection();
            SqlCommand command = connection.CreateCommand();
            command.CommandType = CommandType.StoredProcedure;
            command.CommandText = "procPR_SystemInfo_Select";
            command.Parameters.Add(new SqlParameter("@Id", id));

            try
            {
                connection.Open();

                using (SqlDataReader reader = command.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        systemInfo = CreateEntityByReader(reader);
                    }
                }
            }
            finally
            {
                ConnectionProvider.CloseConnection(connection);
            }

            return systemInfo;
        }

        public void Insert(SystemInfo systemInfo)
        {
            SqlConnection connection = ConnectionProvider.CreateConnection();
            SqlCommand command = connection.CreateCommand();
            command.CommandType = CommandType.StoredProcedure;
            command.CommandText = "procPR_SystemInfo_Insert";
            command.Parameters.Add(new SqlParameter("@Id", systemInfo.Id));
            command.Parameters.Add(new SqlParameter("@SystemName", systemInfo.SystemName));
            command.Parameters.Add(new SqlParameter("@Description", systemInfo.Description));
            command.Parameters.Add(new SqlParameter("@Url", systemInfo.Url));
            command.Parameters.Add(new SqlParameter("@AutoSignIn", systemInfo.AutoSignIn));
            command.Parameters.Add(new SqlParameter("@IsActive", systemInfo.IsActive));

            foreach (SqlParameter parameter in command.Parameters)
            {
                if (parameter.Value == null || parameter.Value.ToString() == Guid.Empty.ToString()) parameter.Value = DBNull.Value;
            }

            try
            {
                connection.Open();
                command.ExecuteNonQuery();
            }
            finally
            {
                ConnectionProvider.CloseConnection(connection);
            }
        }

        public void Update(SystemInfo systemInfo)
        {
            if (systemInfo.UpdateTimestamp == DateTime.MinValue) systemInfo.UpdateTimestamp = DateTime.Now;

            SqlConnection connection = ConnectionProvider.CreateConnection();
            SqlCommand command = connection.CreateCommand();
            command.CommandType = CommandType.StoredProcedure;
            command.CommandText = "procPR_SystemInfo_Update";
            command.Parameters.Add(new SqlParameter("@Id", systemInfo.Id));
            command.Parameters.Add(new SqlParameter("@SystemName", systemInfo.SystemName));
            command.Parameters.Add(new SqlParameter("@Description", systemInfo.Description));
            command.Parameters.Add(new SqlParameter("@Url", systemInfo.Url));
            command.Parameters.Add(new SqlParameter("@AutoSignIn", systemInfo.AutoSignIn));
            command.Parameters.Add(new SqlParameter("@IsActive", systemInfo.IsActive));
            command.Parameters.Add(new SqlParameter("@UpdateTimestamp", DateTime.Now));

            foreach (SqlParameter parameter in command.Parameters)
            {
                if (parameter.Value == null || parameter.Value.ToString() == Guid.Empty.ToString()) parameter.Value = DBNull.Value;
            }

            try
            {
                connection.Open();
                command.ExecuteNonQuery();
            }
            finally
            {
                ConnectionProvider.CloseConnection(connection);
            }
        }

        public void Delete(Guid id)
        {
            SqlConnection connection = ConnectionProvider.CreateConnection();
            SqlCommand command = connection.CreateCommand();
            command.CommandType = CommandType.StoredProcedure;
            command.CommandText = "procPR_SystemInfo_Delete";
            command.Parameters.Add(new SqlParameter("@Id", id));

            try
            {
                connection.Open();
                command.ExecuteNonQuery();
            }
            finally
            {
                ConnectionProvider.CloseConnection(connection);
            }
        }

        #region Private Method

        internal static SystemInfo CreateEntityByReader(IDataReader reader)
        {
            SystemInfo systemInfo = new SystemInfo();
            if (reader["Id"] != DBNull.Value) systemInfo.Id = (Guid)reader["Id"];
            if (reader["SystemName"] != DBNull.Value) systemInfo.SystemName = reader["SystemName"] as string;
            if (reader["Description"] != DBNull.Value) systemInfo.Description = reader["Description"] as string;
            if (reader["Url"] != DBNull.Value) systemInfo.Url = reader["Url"] as string;
            if (reader["AutoSignIn"] != DBNull.Value) systemInfo.AutoSignIn = (Boolean)reader["AutoSignIn"];
            if (reader["IsActive"] != DBNull.Value) systemInfo.IsActive = (Boolean)reader["IsActive"];
            if (reader["InsertTimestamp"] != DBNull.Value) systemInfo.InsertTimestamp = (DateTime)reader["InsertTimestamp"];
            if (reader["UpdateTimestamp"] != DBNull.Value) systemInfo.UpdateTimestamp = (DateTime)reader["UpdateTimestamp"];

            return systemInfo;
        }

        #endregion
    }
}